package com.dope_lh.myproject.demo4;

import com.dope_lh.myproject.request.ApiService;
import com.dope_lh.myproject.request.WeatherBean;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by dope_lh on 17-12-7.
 * M层
 */

public class RequestMode4 {

    private static final String BASE_URL = "http://www.weather.com.cn/";
    private Call<WeatherBean> weatherBeanCall;

    //http://www.weather.com.cn/data/cityinfo/101010100.html
    public void request(String detailId, Callback<WeatherBean> callback) {
        //请求接口
        Retrofit retrofit = new Retrofit.Builder()
                //代表root地址
                .baseUrl(BASE_URL).addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create()).build();

        ApiService apiService = retrofit.create(ApiService.class);

        //请求
        weatherBeanCall = apiService.requestWeather(detailId);

        weatherBeanCall.enqueue(callback);
    }

    /**
     * 取消网络请求
     */
    public void interruptHttp() {
        if (weatherBeanCall != null && !weatherBeanCall.isCanceled()) {
            weatherBeanCall.cancel();
        }
    }
}
